package com.example.meituan.activity.user.adapter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.meituan.R;
import com.example.meituan.activity.bean.AdminBean;
import com.example.meituan.activity.bean.ImgBean;
import com.example.meituan.activity.bean.ShopBean;
import com.example.meituan.activity.bean.ShopCarBean;
import com.example.meituan.activity.bean.TypeBean;
import com.example.meituan.activity.man.frament.OnItemClickListener;
import com.example.meituan.activity.user.OnShopItemClickListener;
import com.example.meituan.dao.AdminDao;
import com.example.meituan.dao.ImgDao;
import com.example.meituan.dao.ShopCarDao;
import com.example.meituan.dao.ShopDao;
import com.example.meituan.dao.TypeDao;

import java.io.File;
import java.util.List;

public class ShopCarAdapter extends RecyclerView.Adapter<ShopCarAdapter.ShopCarViewHolder>{

    private List<ShopCarBean> shopCarList;
    private static OnShopItemClickListener listener;
    public ShopCarAdapter(List<ShopCarBean> shopCarList,OnShopItemClickListener listener) {
        this.shopCarList =shopCarList;
        this.listener=listener;
    }

    public ShopCarAdapter(List<ShopCarBean> shopCarList) {
        this.shopCarList =shopCarList;
    }
    @NonNull
    @Override
    public ShopCarAdapter.ShopCarViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.shop_car_list, parent, false);
        return new ShopCarAdapter.ShopCarViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ShopCarAdapter.ShopCarViewHolder holder, int position) {
        ShopCarBean shopCarBean = shopCarList.get(position);
        holder.bind(shopCarBean);
    }

    public static class ShopCarViewHolder extends RecyclerView.ViewHolder {

        private TextView shopCarName, shopCarShopName, shopCarType, shopCarTotalPrice, shopCarProductNum;
        private ImageView shopCarStore, shopCarShopImg;
        private CheckBox shopCarCheck;

        public ShopCarViewHolder(@NonNull View itemView) {
            super(itemView);
            shopCarName = itemView.findViewById(R.id.shop_car_name);
            shopCarStore = itemView.findViewById(R.id.shop_car_to_store);
            shopCarCheck = itemView.findViewById(R.id.shop_car_check);
            shopCarShopImg = itemView.findViewById(R.id.shop_car_shop_img);
            shopCarShopName = itemView.findViewById(R.id.shop_car_shop_name);
            shopCarType = itemView.findViewById(R.id.shop_car_type);
            shopCarTotalPrice = itemView.findViewById(R.id.shop_car_total_price);
            shopCarProductNum = itemView.findViewById(R.id.shop_car_product_num);
        }

        public void bind(ShopCarBean shopCarBean) {
            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onShopItemClick(shopCarBean);
                }
            });
            ShopBean shopBean=ShopDao.getShopById(shopCarBean.getsShopId());
            shopCarName.setText(shopBean.getName());
            List<ImgBean>imgBeans= ImgDao.getAllImagesByShopId(shopBean.getId());
            if (imgBeans != null && !imgBeans.isEmpty()) {
                ImgBean firstImgBean = imgBeans.get(0);
                String imagePath = firstImgBean.getImg();

                File imageFile = new File(imagePath);
                if (imageFile.exists()) {

                    Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
                    if (bitmap != null) {
                        shopCarShopImg.setImageBitmap(bitmap);
                    }

                }
            }
            shopCarShopName.setText(shopBean.getName());
            if (!TextUtils.isEmpty(shopCarBean.getsType())){
                TypeBean typeBean= TypeDao.getTypeById(shopCarBean.getsType());
                shopCarType.setText(typeBean.getTypeDescription());
            }
            shopCarProductNum.setText("x"+ShopCarDao.CountByShopId(shopBean.getId()));
            int a=Integer.parseInt(shopBean.getPrice())*ShopCarDao.CountByShopId(shopBean.getId());
            shopCarTotalPrice.setText("￥"+a);
            shopCarStore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener!=null){
                        listener.toStore(shopCarBean);
                    }
                }
            });

            shopCarCheck.setOnCheckedChangeListener(null); // 防止无限递归

            shopCarCheck.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if (listener != null) {
                    listener.onCheckedChanged(shopCarBean, isChecked);
                }
            });



        }

    }
    @Override
    public int getItemCount() {
        return shopCarList.size();
    }
}
